package com.jaya.budan.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jaya.budan.R;
import com.jaya.budan.bean.ListUser_Information;

import java.util.List;

public class Square_ViewPagerAdapter extends PagerAdapter {

    private Context mcontext;
    private List<ListUser_Information.Files> files;
    private View view;

    public Square_ViewPagerAdapter(Context context, List<ListUser_Information.Files> file) {
        mcontext = context;
        files = file;
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return super.getItemPosition(object);
    }

    @Override
    public int getCount() {
        return files.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//        super.destroyItem(container, position, object);
        container.removeView(view);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        view = View.inflate(mcontext, R.layout.gv_photoalbum_item2, null);
        ImageView iv_photo_album_icon = view.findViewById(R.id.iv_photo_album_icon);
        Glide.with(mcontext).load(files.get(position).getPath() + "").apply(new RequestOptions()
                .placeholder(R.mipmap.banner).error(R.mipmap.banner)
                .diskCacheStrategy(DiskCacheStrategy.NONE)//关闭Glide的硬盘缓存机制
        ).into(iv_photo_album_icon);
        container.addView(view);
        return view;
    }
}
